package cn.kgc.controller;


import cn.kgc.commons.annoation.IgnoreResultAdviceAnnotation;
import cn.kgc.commons.constant.HttpExceptionCodeEnum;
import cn.kgc.commons.exception.HttpException;
import cn.kgc.commons.util.RedisUtil;
import cn.kgc.base.entity.JwtToken;
import cn.kgc.base.dto.UmsMemberDTO;
import cn.kgc.entity.UmsMember;
import cn.kgc.service.JwtService;
import cn.kgc.service.UmsMemberService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static cn.kgc.commons.constant.SystemConstant.REGISTER_PHONE_CODE;

/**
 * <p>认证控制器</p>
 *
 * @author 卢传琪
 * @since 2022年10月17日
 */
@RestController
@Api(tags = "认证接口")
@RequestMapping("/auth")
public class AuthorityController {
    @Resource
    private JwtService jwtService;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private UmsMemberService umsMemberService;


    @PostMapping("/getToken")
    @IgnoreResultAdviceAnnotation
    @ApiOperation(value = "登录并获得令牌",httpMethod = "POST",produces = "application/json;charset=utf-8")
    @CrossOrigin
    public JwtToken login(@RequestBody UmsMemberDTO umsMemberDTO) throws Exception{
        String token=jwtService.token(umsMemberDTO.getUsername(),umsMemberDTO.getPassword());
        if (token!=null){
            JwtToken jwtToken=new JwtToken();
            jwtToken.setToken(token);
            return jwtToken;
        }else {
            return null;
        }
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册获得令牌",httpMethod ="POST", produces = "application/json;charset=utf-8", response = JwtToken.class)
    @IgnoreResultAdviceAnnotation
    @CrossOrigin
    public JwtToken register(@RequestBody UmsMemberDTO umsMemberDTO) throws Exception{
        //判断账号是否重复
        LambdaQueryWrapper<UmsMember> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UmsMember::getUsername,umsMemberDTO.getUsername());
        int count=umsMemberService.count(lambdaQueryWrapper);
        if (count>0){
           throw new HttpException(HttpExceptionCodeEnum.USERNAME_REPEAT.getCode(),
                   HttpStatus.INTERNAL_SERVER_ERROR.value());
        }
        //验证码是否正确
        String key= REGISTER_PHONE_CODE+umsMemberDTO.getPhone();
        if (redisUtil.hasKey(key)){
            String tempCode= redisUtil.get(key).toString();
            if (tempCode.equals(umsMemberDTO.getCode())){
                String token=jwtService.register(umsMemberDTO.getUsername(), umsMemberDTO.getPassword(), umsMemberDTO.getPhone());
                JwtToken jwtToken=new JwtToken();
                jwtToken.setToken(token);
                return jwtToken;
            }
        }
        throw new HttpException(HttpExceptionCodeEnum.PHONE_CODE_ERROR.getCode(), HttpStatus.INTERNAL_SERVER_ERROR.value());
    }
}
